package com.cssy.xxkj.logic.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.contants.BillTypeConstant;
import com.cssy.xxkj.logic.dao.LogicAllocatInwhDAO;
import com.cssy.xxkj.logic.dao.LogicAllocatInwhItemDAO;
import com.cssy.xxkj.logic.entity.LogicAllocatInwhDO;
import com.cssy.xxkj.logic.entity.LogicAllocatInwhItemDO;
import com.cssy.xxkj.logic.service.ILogicAllocatInwhService;
import com.cssy.xxkj.logic.service.ILogicHsUtilService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicAllocatInwhService")
public class LogicAllocatInwhServiceImpl implements ILogicAllocatInwhService{

	@Resource(name = "logicAllocatInwhDAO")
	private LogicAllocatInwhDAO logicAllocatInwhDAO;
	
	@Resource(name = "logicAllocatInwhItemDAO")
	private LogicAllocatInwhItemDAO logicAllocatInwhItemDAO;
	
	@Resource(name = "logicHsUtilService")
	private ILogicHsUtilService logicHsUtilService;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs = new SearchResult(scs);
		List<LogicAllocatInwhDO> list = logicAllocatInwhDAO.find(scs);
		rs.setRecordCount(logicAllocatInwhDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		return logicAllocatInwhDAO.deleteLogicAllocatInwhDOByPrimaryKey(id);
	}

	@Override
	public Integer update(LogicAllocatInwhDO inwhDO, User user)
			throws SyBaseException {
		inwhDO.setBizModifyName(user.getName());
		inwhDO.setBizModifyDate(new Date());
		inwhDO.setBizVersionMark(inwhDO.getBizVersion());
		inwhDO.setBizVersion(inwhDO.getBizVersion()+Integer.valueOf(1));
		return logicAllocatInwhDAO.updateLogicAllocatInwhDO(inwhDO);
	}

	@Override
	public LogicAllocatInwhDO save(LogicAllocatInwhDO inwhDO, User user)
			throws SyBaseException {
		inwhDO.setBizCreatorName(user.getName());
		inwhDO.setBizCreatorDate(new Date());
		inwhDO.setBizVersion(Integer.valueOf(1));
		logicAllocatInwhDAO.insertLogicAllocatInwhDO(inwhDO);
		return inwhDO;
	}
	
	@Override
	public void allocatInwhSave(HttpServletRequest request, LogicAllocatInwhDO curAP)
			throws SyBaseException{
		String zd005InDate = request.getParameter("zd005InDate");
		String flagMark = request.getParameter("flagMark");
		String dataItem = request.getParameter("dataItem");
		String datadele = request.getParameter("datadele");
		try{
			curAP.setZd005InDate(DateUtils.stringToDate(zd005InDate));
			if(BillStatusUtil.INWH_BILL_STATUS_FIVE.equals(flagMark)){
				//驳回
				LogicAllocatInwhDO aInwh = logicAllocatInwhDAO.findLogicAllocatInwhDOByPrimaryKey(curAP.getId());
				if(aInwh==null){
					throw new SyBaseException("没有找到数据，请重新提交！");
				}
				aInwh.setZd002Status(flagMark);
				this.update(aInwh, SessionUtil.getUser(request));
			}else{
				String aInwhId = "";
				curAP.setZd002Status(flagMark);
				if(curAP==null || StringUtils.isBlank(curAP.getId())){
					aInwhId = IDCodeTypeUtil.getId();
					curAP.setId(aInwhId);
					curAP.setZd002InCode(syCodeService.getCode(BillTypeConstant.ALLOCATINWH,BillTypeConstant.ALLOCATINWH,SyCode.DATE_YYMM,5));
					this.save(curAP, SessionUtil.getUser(request));
				}else{
					aInwhId = curAP.getId();
					Integer row = this.update(curAP, SessionUtil.getUser(request));
					if(Integer.valueOf(1).equals(row)){
						if(StringUtils.isNotBlank(datadele)){
							List<LogicAllocatInwhItemDO> aInwhItemList = JSONArray
									.parseArray(datadele,LogicAllocatInwhItemDO.class);
							processAlloactInwhItemList(aInwhItemList);
						}
					}else{
						throw new SyBaseException("数据有错，请重新操作！");
					}
				}
				//新增、修改、修改库存
				List<LogicAllocatInwhItemDO> _aInwhItemList = JSONArray.parseArray(dataItem,LogicAllocatInwhItemDO.class);
				processAlloactInwhItemList(_aInwhItemList, flagMark, aInwhId, SessionUtil.getUser(request));
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()+ e.getMessage());
		}
	}
	
	/**
	 * 删除调拨接收的行记录
	 * @param aInwhItemList
	 */
	private void processAlloactInwhItemList(List<LogicAllocatInwhItemDO> aInwhItemList){
		for(LogicAllocatInwhItemDO aInwhItem :aInwhItemList){
			if(aInwhItem==null||StringUtils.isBlank(aInwhItem.getId())){
				continue;
			}
			logicAllocatInwhItemDAO.deleteLogicAllocatInwhItemDOByPrimaryKey(aInwhItem.getId());
		}
	}
	
	/**
	 * 新增、修改接收行记录信息
	 * @param _aInwhItemList
	 * @param flagMark
	 * @param aInwhId
	 * @param user
	 * @throws SyBaseException
	 */
	private void processAlloactInwhItemList(
			List<LogicAllocatInwhItemDO> aInwhItemList, String flagMark,
			String aInwhId, User user) throws SyBaseException {
		try{
			if(BillStatusUtil.INWH_BILL_STATUS_FOUR.equals(flagMark)){
				LogicAllocatInwhDO aInwh = logicAllocatInwhDAO.findLogicAllocatInwhDOByPrimaryKey(aInwhId);
				//审批通过，需改变库存
				for(LogicAllocatInwhItemDO aInwhItem : aInwhItemList){
					if(aInwhItem==null || StringUtils.isBlank(aInwhItem.getId()))
						continue;
					logicAllocatInwhItemDAO.updateLogicAllocatInwhItemDO(aInwhItem);
					//修改库存
					logicHsUtilService.processHsAllocatInwhItem(aInwh, aInwhItem, user);
				}
			}else{
				for(LogicAllocatInwhItemDO aInwhItem : aInwhItemList){
					if(aInwhItem==null)
						continue;
					if(StringUtils.isBlank(aInwhItem.getId())){
						aInwhItem.setId(IDCodeTypeUtil.getId());
						aInwhItem.setZd001AllocatInId(aInwhId);
						logicAllocatInwhItemDAO.insertLogicAllocatInwhItemDO(aInwhItem);
					}else{
						logicAllocatInwhItemDAO.updateLogicAllocatInwhItemDO(aInwhItem);
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()+ e.getMessage());
		}
	}

	@Override
	public LogicAllocatInwhDO findListById(String id) throws SyBaseException {
		return logicAllocatInwhDAO.findLogicAllocatInwhDOByPrimaryKey(id);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return logicAllocatInwhDAO.deleteBizRemoved(id);
	}

}
